package com.sccba.emtbusiness.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.sccba.common.annotation.DataScope;
import com.sccba.common.utils.DateUtils;
import com.sccba.emtbusiness.domain.YjczItemChannel;
import com.sccba.emtbusiness.service.IYjczItemChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczImportantItemMapper;
import com.sccba.emtbusiness.domain.YjczImportantItem;
import com.sccba.emtbusiness.service.IYjczImportantItemService;

/**
 * 重大发版事项Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-25
 */
@Service
public class YjczImportantItemServiceImpl implements IYjczImportantItemService 
{
    @Autowired
    private YjczImportantItemMapper yjczImportantItemMapper;

    /**
     * 查询重大发版事项
     * 
     * @param pkId 重大发版事项主键
     * @return 重大发版事项
     */
    @Override
    public YjczImportantItem selectYjczImportantItemByPkId(Long pkId)
    {
        return yjczImportantItemMapper.selectYjczImportantItemByPkId(pkId);
    }

    /**
     * 查询重大发版事项列表
     * 
     * @param yjczImportantItem 重大发版事项
     * @return 重大发版事项
     */
    @Override
//    @DataScope(scopeByChannel = true, channelAlias = "c")
    public List<YjczImportantItem> selectYjczImportantItemList(YjczImportantItem yjczImportantItem)
    {
        List<YjczImportantItem> list = yjczImportantItemMapper.selectYjczImportantItemList(yjczImportantItem);
        if (!list.isEmpty()) {
            List<Long> itemIds = list.stream().map(item -> item.getPkId()).collect(Collectors.toList());
            List<YjczItemChannel> itemChannels = yjczItemChannelService.selectItemChannelByItemId(itemIds);
            Map<Integer, Map<Long, String>> itemIdsAndChannelNames = itemChannels.stream().collect(
                    Collectors.groupingBy(
                            YjczItemChannel::getType,
                            Collectors.groupingBy(
                                    YjczItemChannel::getItemId, Collectors.mapping(YjczItemChannel::getChannelName, Collectors.joining(","))
                            )
                    )
            );
            Map<Long, String> channelNameMap = itemIdsAndChannelNames.get(CHANNEL_ID_TYPE);
            Map<Long, String> onlineChannelNameMap = itemIdsAndChannelNames.get(ONLINE_CHANNEL_ID_TYPE);
            Map<Long, String> effectChannelNameMap = itemIdsAndChannelNames.get(EFFECT_CHANNEL_ID_TYPE);
            for (YjczImportantItem item : list) {
                Long itemId = item.getPkId();
                if (null != channelNameMap) {
                    item.setChannelName(channelNameMap.get(itemId));
                }
                if (null != onlineChannelNameMap) {
                    item.setItemOnlineChannel(onlineChannelNameMap.get(itemId));
                }
                if (null != effectChannelNameMap) {
                    item.setItemEffectChannel(effectChannelNameMap.get(itemId));
                }
            }
        }
        return list;
    }

    private static final Integer CHANNEL_ID_TYPE = 0;

    private static final Integer ONLINE_CHANNEL_ID_TYPE = 1;

    private static final Integer EFFECT_CHANNEL_ID_TYPE = 2;

    @Autowired
    private IYjczItemChannelService yjczItemChannelService;

    /**
     * 新增重大发版事项
     * 
     * @param yjczImportantItem 重大发版事项
     * @return 结果
     */
    @Override
    public int insertYjczImportantItem(YjczImportantItem yjczImportantItem)
    {
        yjczImportantItem.setCreateTime(DateUtils.getNowDate());
        int i = yjczImportantItemMapper.insertYjczImportantItem(yjczImportantItem);
        insertItemChannelList(yjczImportantItem);
        return i;
    }

    /**
     * 修改重大发版事项
     * 
     * @param yjczImportantItem 重大发版事项
     * @return 结果
     */
    @Override
    public int updateYjczImportantItem(YjczImportantItem yjczImportantItem)
    {
        yjczImportantItem.setUpdateTime(DateUtils.getNowDate());
        int i = yjczImportantItemMapper.updateYjczImportantItem(yjczImportantItem);
        yjczItemChannelService.deleteByItemId(yjczImportantItem.getPkId());
        insertItemChannelList(yjczImportantItem);
        return i;
    }

    private void insertItemChannelList (YjczImportantItem yjczImportantItem) {
        Long itemId = yjczImportantItem.getPkId();
        List<Long> itemChannelIds = yjczImportantItem.getItemChannelIds();
        List<Long> itemOnlineChannelIds = yjczImportantItem.getItemOnlineChannelIds();
        List<Long> itemEffectChannelIds = yjczImportantItem.getItemEffectChannelIds();
        List<YjczItemChannel> list = new ArrayList<>();
        putInItemChannelList(list, itemId, CHANNEL_ID_TYPE, itemChannelIds);
        putInItemChannelList(list, itemId, ONLINE_CHANNEL_ID_TYPE, itemOnlineChannelIds);
        putInItemChannelList(list, itemId, EFFECT_CHANNEL_ID_TYPE, itemEffectChannelIds);
        if (!list.isEmpty()) {
            yjczItemChannelService.batchInsert(list);
        }
    }

    private void putInItemChannelList (List<YjczItemChannel> list, Long itemId, Integer type, List<Long> channelIds) {
        YjczItemChannel itemChannel;
        for (Long channelId : channelIds) {
            itemChannel = new YjczItemChannel();
            itemChannel.setItemId(itemId);
            itemChannel.setChannelId(channelId);
            itemChannel.setType(type);
            list.add(itemChannel);
        }
    }

    /**
     * 批量删除重大发版事项
     * 
     * @param pkIds 需要删除的重大发版事项主键
     * @return 结果
     */
    @Override
    public int deleteYjczImportantItemByPkIds(Long[] pkIds)
    {
        return yjczImportantItemMapper.deleteYjczImportantItemByPkIds(pkIds);
    }

    /**
     * 删除重大发版事项信息
     * 
     * @param pkId 重大发版事项主键
     * @return 结果
     */
    @Override
    public int deleteYjczImportantItemByPkId(Long pkId)
    {
        return yjczImportantItemMapper.deleteYjczImportantItemByPkId(pkId);
    }
}
